'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	const {
		user_id,
		article_id
	} = event
	//查询对应user_id的用户
	const userInfo = await db.collection("user").doc(user_id).get()
	//更新对用article_id的
	// await db.collection('user').doc(user_id).update({
	// 	article_likes_ids:dbCmd.addToSet(article_id)
	// })
	const article_likes_ids = userInfo.data[0].article_likes_ids
	let dbCmdFuns = null
	if(article_likes_ids.includes(article_id)){
		dbCmdFuns =  dbCmd.pull(article_id)
	}else{
		dbCmdFuns = dbCmd.addToSet(article_id)
	}
	await db.collection('user').doc(user_id).update({
		article_likes_ids:dbCmdFuns
	})
	
	//返回数据给客户端
	return {
		code:200,
		msg:"收藏成功",
		data:userInfo[0]
	}
};
